using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Mvc.Ajax;
using ComputerShopE3.Models;
using System.Data;

namespace ComputerShopE3.Controllers
{
    public class EmployeeController : Controller
    {
        //
        // GET: /Employee/
        ComputerShopDBEntities1 _db = new ComputerShopDBEntities1();
        DESKTOP CustomDesktop = new DESKTOP();

        public ActionResult Index()
        {
            return View();
        }

        //
        // GET: /Employee/Details/5

        public ActionResult Details(int id)
        {
            return View();
        }

        //
        // GET: /Employee/Create

        public ActionResult Create()
        {
            return View();
        } 

        //
        // POST: /Employee/Create

        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Create(FormCollection collection)
        {
            try
            {
                // TODO: Add insert logic here

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        //
        // GET: /Employee/Edit/5
 
        public ActionResult Edit(int id)
        {
            return View();
        }

        //
        // POST: /Employee/Edit/5

        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Edit(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add update logic here
 
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        public ActionResult ProductManage()
        {
            ViewData.Model = _db.LOAI_SAN_PHAM.ToList();
            return View();
        }

        public ActionResult AllProductManage()
        {
            ViewData.Model = _db.SAN_PHAM.ToList();
            return View();
        }


        #region ProductTypeManage

        public ActionResult CASEManage()
        {
            ViewData.Model = _db.CASE.ToList();
            return View();
        }

        public ActionResult CD_DVDManage()
        {
            ViewData.Model = _db.CD_DVD.ToList();
            return View();
        }

        public ActionResult CPUManage()
        {
            ViewData.Model = _db.CPU.ToList();
            return View();
        }

        public ActionResult DesktopManage()
        {
            ViewData.Model = _db.DESKTOP.ToList();
            return View();
        }

        public ActionResult HDDManage()
        {
            ViewData.Model = _db.HDD.ToList();
            return View();
        }

        public ActionResult HE_DIEU_HANHManage()
        {
            ViewData.Model = _db.HE_DIEU_HANH.ToList();
            return View();
        }

        public ActionResult KEYBOARDManage()
        {
            ViewData.Model = _db.KEYBOARD.ToList();
            return View();
        }

        public ActionResult LAPTOPManage()
        {
            ViewData.Model = _db.LAPTOP.ToList();
            return View();
        }

        public ActionResult MAINBOARDManage()
        {
            ViewData.Model = _db.MAINBOARD.ToList();
            return View();
        }

        public ActionResult MONITORManage()
        {
            ViewData.Model = _db.MONITOR.ToList();
            return View();
        }

        public ActionResult MOUSEManage()
        {
            ViewData.Model = _db.MOUSE.ToList();
            return View();
        }

        public ActionResult PINManage()
        {
            ViewData.Model = _db.PIN.ToList();
            return View();
        }

        public ActionResult POWERManage()
        {
            ViewData.Model = _db.POWER.ToList();
            return View();
        }

        public ActionResult RAMManage()
        {
            ViewData.Model = _db.RAM.ToList();
            return View();
        }

        public ActionResult SOUND_CARDManage()
        {
            ViewData.Model = _db.SOUND_CARD.ToList();
            return View();
        }

        public ActionResult SPEAKERManage()
        {
            ViewData.Model = _db.SPEAKER.ToList();
            return View();
        }

        public ActionResult VGAManage()
        {
            ViewData.Model = _db.VGA.ToList();
            return View();
        }

        #endregion


        public ActionResult CreateCASE()
        {
            return View();
        }


        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult CreateCASE(FormCollection form)
        {
            try
            {
                // TODO: Add insert logic here
                //string id = form.Get("id");
                string KichThuoc = form.Get("KichThuoc");
                string VatLieu = form.Get("VatLieu");
                string HoTro = form.Get("HoTro");
                string Nguon = form.Get("Nguon");
                string CongGiaoTiep = form.Get("CongGiaoTiep");
                string KhongGianChua = form.Get("KhongGianChua");
                string MoRong = form.Get("MoRong");
                string TanNhiet = form.Get("TanNhiet");

                CASE caseToAdd = new CASE
                {
                    KichThuoc = KichThuoc,
                    VatLieu = VatLieu,
                    HoTro = HoTro,
                    Nguon = Nguon,
                    CongGiaoTiep = CongGiaoTiep,
                    KhongGianChua = KhongGianChua,
                    MoRong = MoRong,
                    TanNhiet = TanNhiet,
                };

                SAN_PHAM spToAdd = new SAN_PHAM
                {
                    MoTa = form.Get("MoTa"),
                    LoaiLinhKien = short.Parse(form.Get("LoaiLinkKien")),
                };
                _db.AddToSAN_PHAM(spToAdd);
                //_db.AddToCASE(caseToAdd);
                _db.SaveChanges();
                return RedirectToAction("CASEManage");
            }
            catch(Exception ex)
            {
                return View(ex.Message);
            }
        }

        public ActionResult DeleteCASE(CASE caseToDelete)
        {
            try
            {
                var originalCASE = (from c in _db.CASE
                                       where c.id == caseToDelete.id
                                       select c).FirstOrDefault();
                var originalSP = (from s in _db.SAN_PHAM
                                    where s.id == caseToDelete.id
                                    select s).FirstOrDefault();

                _db.DeleteObject(originalCASE);
                _db.SaveChanges();
                _db.DeleteObject(originalSP);
                _db.SaveChanges();
                return RedirectToAction("CASEManage");
            }
            catch
            {
                return RedirectToAction("CASEManage");
            }

        }

        public ActionResult CustomizeProduct()
        {
            ViewData.Model = _db.CPU.ToList();
            return View();
        }

        public ActionResult ChooseMAINBOARD()
        {
            ViewData.Model = _db.MAINBOARD.ToList();
            return View();
        }
        public void SelectCPU(int id)
        {
            var Desktop = new DESKTOP();
            Desktop.CPU = (from c in _db.CPU where c.id == id
                                       select c).FirstOrDefault();

        }

        public ActionResult CreateHDH()
        {
            return View();
        }

        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult CreateHDH(FormCollection form)
        {
            try
            {
                // TODO: Add insert logic here
                string id = form.Get("id");
                string Ten = form.Get("Ten");


                HE_DIEU_HANH HDHToAdd = new HE_DIEU_HANH
                {
                    Ten = Ten,
                };
                SAN_PHAM spToAdd = new SAN_PHAM
                {
                    MoTa = "Test",
                    TenSanPham = "asd",
                    Gia = 112,
                    BaoHanh = "asd",
                    NgayThemSP = DateTime.Now,
                    SoLuongTon = 5,
                    idKhuyenMai = 2,
                };

                HDHToAdd.SAN_PHAM = spToAdd;
                spToAdd.LOAI_SAN_PHAMReference.EntityKey = new EntityKey("LOAI_SAN_PHAM", "id",1);
                //spToAdd.KHUYEN_MAI = c.First();
                _db.AddToSAN_PHAM(spToAdd);
                //_db.AddToHE_DIEU_HANH(HDHToAdd);
                _db.SaveChanges();
                return RedirectToAction("HE_DIEU_HANHManage");
            }
            catch (Exception ex)
            {
                return View(ex.Message);
            }
        }
    }
}
